home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / C Internet Config / IC TextEdit Patch ƒ / ICeTEe.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-06  |  8.7 KB  |  466 lines  |  [TEXT/SPM ]

  1. /*
  2.     ICeTEe.c
  3.     
  4.     C translation of the ICeTEe.p pascal source file.
  5. */
  6.  
  7. #include <Processes.h>
  8. #include <LowMem.h>
  9. #include <Notification.h>
  10. #include <Traps.h>
  11. #include <GestaltEqu.h>
  12. #include <OSUtils.h>
  13. #include <Types.h>
  14. #include <TextEdit.h>
  15.  
  16. #if defined(powerc) || defined (__powerc)
  17. #include <stdlib.h>
  18. #else
  19. #include <SetUpA4.h>
  20. #endif
  21.  
  22. #include "ICeTEe.h"
  23. #include "ShowInitIcon.h"
  24.  
  25. #include "ICTypes.h"
  26. #include "IC Component API.h"
  27.  
  28. static icteGlobalsPtr gGlobalsPtr;
  29.  
  30. #define SetMyGlobals(globs) (gGlobalsPtr=((icteGlobalsPtr)(globs)))
  31. #define GetMyGlobals() (gGlobalsPtr)
  32.  
  33. static pascal OSErr MyGestalt(OSType selector,long* response){
  34.     
  35.     *response=(long)GetMyGlobals();
  36.     return noErr;
  37. }
  38.  
  39. static void CallTEClick(Point pt,Boolean fExtend,TEHandle teh,TEClickUPP tec){
  40.     
  41.     CallTEClickProc(tec,pt,fExtend,teh);
  42. }
  43.  
  44. static pascal void MyNMResponseProc(NMRecPtr nm){
  45.     THz ozone;
  46.     Handle strh;
  47.     OSErr junk;
  48.     
  49.     junk=NMRemove(nm);
  50.     ozone=GetZone();
  51.     SetZone(SystemZone());
  52.     
  53.     strh=RecoverHandle((Ptr)nm->nmStr);
  54.     
  55.     if (strh!=(Handle)0){
  56.         DisposeHandle(strh);
  57.     }
  58.     
  59.     DisposePtr((Ptr)nm);
  60.     SetZone(ozone);
  61.     
  62. }
  63.  
  64. static Boolean CurrentProcessExcluded(void){
  65.     ProcessSerialNumber PSN;
  66.     ProcessInfoRec info;
  67.     exHandle exclusions;
  68.     short i;
  69.     icteGlobalsPtr globals=GetMyGlobals();
  70.     
  71.     PSN.highLongOfPSN=0;
  72.     PSN.lowLongOfPSN=kCurrentProcess;
  73.     info.processInfoLength=sizeof(ProcessInfoRec);
  74.     info.processName=(StringPtr)0;
  75.     info.processAppSpec=(FSSpecPtr)0;
  76.     
  77.     if (GetProcessInformation(&PSN,&info)==noErr){
  78.         exclusions=globals->exclusions;
  79.         for (i=0;i<((GetHandleSize((Handle)exclusions))/4);i++){
  80.             if ((*exclusions)[i]==info.processSignature)
  81.                 return true;
  82.         }
  83.     }
  84.     
  85.     return false;
  86. }
  87.  
  88. static Boolean HaveComponentManager(void){
  89.     long resp;
  90.     
  91.     return (Gestalt(gestaltComponentMgr,&resp)==noErr);
  92. }
  93.  
  94. static ICError DoCommandClick(TEHandle teh,long selStart,long selEnd){
  95.     ComponentInstance inst;
  96.     ICError err;
  97.     ICError err2;
  98.     Handle text;
  99.     SignedByte s;
  100.     RgnHandle rgn;
  101.     short i;
  102.     long junklong;
  103.     Str31 hint="\pmailto",at="\p@";
  104.     Handle urlh;
  105.     
  106.     if (HaveComponentManager()){
  107.         err=ICCStart(&inst,kCreator);
  108.     } else {
  109.         err=noCMErr;
  110.     }
  111.     if (err==noErr){
  112.         err=ICCFindConfigFile(inst,0,nil);
  113.         if (err==noErr){
  114.             text=(Handle)TEGetText(teh);
  115.             s=HGetState(text);
  116.             HLock(text);
  117.             urlh=NewHandle(0);
  118.             
  119.             err=ICCParseURL(inst,hint,*text,GetHandleSize(text),&selStart,&selEnd,urlh);
  120.             
  121.             if (err==noErr){
  122.                 hint[0]=0;
  123.                 
  124.                 if (Munger(urlh,0,&at[1],at[0],nil,0)>=0){
  125.                     hint[0]=6;
  126.                     hint[1]='m';hint[2]='a';hint[3]='i';
  127.                     hint[4]='l';hint[5]='t';hint[6]='o';
  128.                 }
  129.                 
  130.                 err=ICCLaunchURL(inst,hint,*text,GetHandleSize(text),&selStart,&selEnd);
  131.             }
  132.             DisposeHandle(urlh);
  133.             TESetSelect(selStart,selEnd,teh);
  134.             
  135.             if (err==noErr){
  136.                 for (i=0;i<LMGetMenuFlash();i++){
  137.                     Delay(5,&junklong);
  138.                     TEDeactivate(teh);
  139.                     Delay(5,&junklong);
  140.                     TEActivate(teh);
  141.                 }
  142.             }
  143.             
  144.             HSetState(text,s);
  145.         }
  146.         err2=ICCStop(inst);
  147.         if (err==noErr)
  148.             err=err2;
  149.     }
  150.     
  151.     return err;
  152. }
  153.  
  154. static void GetIndStrH(Handle h,short index,StringPtr str){
  155.     short count,i;
  156.     long ps;
  157.     short ** sh=(short**)h;
  158.     unsigned char *ucp,uc;
  159.     
  160.     count=**sh;
  161.     
  162.     if ((1<=index)&&(index<=count)){
  163.         ps=(long)sizeof(short);
  164.         ucp=(unsigned char*)(*h);
  165.             
  166.         for (i=0;i<index;i++){
  167.             ucp=(unsigned char*)((*h)+ps);
  168.             ps += *ucp +1;
  169.         }
  170.         
  171.         ucp=(unsigned char*)((*h)+ps);
  172.         
  173.         BlockMove((Ptr)((*h)+ps),str,*ucp+1);
  174.     } else {
  175.         str[0]=0;
  176.     }
  177. }
  178.  
  179. static void MyTEClick(TEHandle teh,long old_selStart,long old_selEnd){
  180.     ICError err;
  181.     Str255 message;
  182.     Str255 msg;
  183.     NMRecPtr nm;
  184.     short strindex;
  185.     StringHandle strh;
  186.     icteGlobalsPtr globals=GetMyGlobals();
  187.     unsigned char* cp,* mcp;
  188.     
  189.     if (!CurrentProcessExcluded()){
  190.         if (!((old_selStart<=(*teh)->selStart)&&((*teh)->selStart<=old_selEnd)
  191.                 &&(old_selStart<=(*teh)->selEnd)&&((*teh)->selEnd<=old_selEnd))){
  192.             old_selStart=(*teh)->selStart;
  193.             old_selEnd=(*teh)->selEnd;
  194.         }
  195.         
  196.         err=DoCommandClick(teh,old_selStart,old_selEnd);
  197.         
  198.         if (err!=noErr){
  199.             switch(err){
  200.                 case badComponentInstance:
  201.                     strindex=strNoICErr;
  202.                     break;
  203.                 case noCMErr:
  204.                     strindex=strNoCMErr;
  205.                     break;
  206.                 case badComponentSelector:
  207.                     strindex=strInsufficientICErr;
  208.                     break;
  209.                 case memFullErr:
  210.                     strindex=strNoMemoryErr;
  211.                     break;
  212.                 case afpItemNotFound:
  213.                     strindex=strCantFindHelperErr;
  214.                     break;
  215.                 case icPrefNotFoundErr:
  216.                     strindex=strNoHelperErr;
  217.                     break;
  218.                 case icNoURLErr:
  219.                     strindex=strNoURLErr;
  220.                     break;
  221.                 case noPortErr:
  222.                     strindex=strCantHackIt;
  223.                     break;
  224.                 default:
  225.                     strindex=strMiscErr;
  226.                     break;
  227.             }
  228.             
  229.             GetIndStrH(globals->errors,strindex,message);
  230.             
  231.             if (message[0]!=0){
  232.                 NMUPP respUpp;
  233.                 
  234.                 if (strindex==strMiscErr){
  235.                     // replace the ^0 with the error
  236.                     NumToString(err,msg);
  237.                     
  238.                     cp=message+1;
  239.                     while (*cp!='^')
  240.                         cp++;
  241.                     
  242.                     // cp points to the ^
  243.                     cp++;cp++;
  244.                     mcp=msg+1;
  245.                     
  246.                     message[0]=message[0]-2+msg[0]; // add the new string len
  247.                     // points at the close paren...
  248.                     while (msg[0]){
  249.                         *cp=*mcp;
  250.                         cp++;
  251.                         mcp++;
  252.                         msg[0]--;
  253.                     }
  254.                     
  255.                     // now add the ).
  256.                     *cp=')';cp++;
  257.                     *cp='.';
  258.                 }
  259.                 
  260.                 strh=NewString(message);
  261.                 
  262.                 HLock((Handle)strh);
  263.                 nm=(NMRecPtr)NewPtrSysClear(sizeof(NMRec));
  264.                 
  265.                 respUpp=NewNMProc(MyNMResponseProc);
  266.                 
  267.                 if (nm!=(NMRecPtr)0){
  268.                     nm->qType=nmType;
  269.                     nm->nmMark=0;
  270.                     nm->nmIcon=(Handle)0;
  271.                     nm->nmSound=(Handle)0;
  272.                     nm->nmStr=*strh;
  273.                     nm->nmResp=respUpp;
  274.                     
  275.                     err=NMInstall(nm);
  276.                 } else {
  277.                     SysBeep(10);
  278.                 }
  279.             }
  280.         }
  281.     }
  282. }
  283.  
  284. static Boolean IsKeyPressed(unsigned short k){
  285.     unsigned char km[16];
  286.     
  287.     GetKeys((long*)km);
  288.     
  289.     return ((km[k>>3]>>(k&7))&1);
  290. }
  291.  
  292. static pascal void PascalTEClickPatch(Point pt,Boolean fExtend,TEHandle teh){
  293.     long old_selStart,old_selEnd;
  294.     icteGlobalsPtr globals;
  295.     THz ozone;
  296.     Boolean command_key;
  297.     long savea4;
  298.     
  299.     // save all of the registers
  300. #if defined(powerc) || defined (__powerc)
  301.     __rsrcinit();
  302. #else
  303.     asm {
  304.         movem.l a0-a6/d0-d6,-(sp)
  305.     }
  306.     SetUpA4();
  307. #endif
  308.     
  309.     globals=GetMyGlobals();
  310.     old_selStart=(*teh)->selStart;
  311.     old_selEnd=(*teh)->selEnd;
  312.     
  313.     command_key=IsKeyPressed(55);
  314.     
  315.     CallTEClick(pt,fExtend,teh,globals->old_teclick);
  316.     
  317.     if (command_key){
  318.         ozone=GetZone();
  319.         SetZone(SystemZone());
  320.         
  321.         MyTEClick(teh,old_selStart,old_selEnd);
  322.         
  323.         SetZone(ozone);
  324.     }
  325.     
  326.     // restore all of the registers
  327. #if defined(powerc) || defined (__powerc)
  328.     __rsrcterm();
  329. #else
  330.     RestoreA4();
  331.     asm {
  332.         movem.l (sp)+,a0-a6/d0-d6
  333.     }
  334. #endif
  335.     
  336. }
  337.  
  338. void main(void){
  339.     Ptr ts;
  340.     Handle res;
  341.     THz ozone;
  342.     OSErr err,err2;
  343.     long response;
  344.     icteGlobalsPtr globals;
  345.     Handle exclusions;
  346.     Handle errors;
  347.     VersRecHndl vers;
  348.     
  349. #if defined(powerc) || defined (__powerc)
  350.     /* PPC C way to handle an INIT */
  351.     __rsrcinit();
  352.     
  353.     ts=(Ptr)PascalTEClickPatch;
  354.     
  355. #else
  356.     /* Standard C/68k way to handle an INIT */
  357.     
  358.     asm {
  359.         move.l a0,ts
  360.     }
  361.     
  362.     RememberA0();
  363.     SetUpA4();
  364.     
  365. #endif
  366.  
  367.     // detach our resource
  368.     
  369.     if (ts!=(Ptr)0){
  370.         res=RecoverHandle(ts);
  371.         
  372.         err=noErr;
  373.         
  374.         if (res!=(Handle)0){
  375.             DetachResource(res);
  376.         } else {
  377.             // RecoverHandle failed.
  378.             err=MemError();
  379.         }
  380.         
  381.         ShowInitIcon(rICTEIcon,false);
  382.         
  383.         ozone=GetZone();
  384.         SetZone(SystemZone());
  385.         
  386.         // err=noErr;
  387.         
  388.         if ((Gestalt(gestaltSystemVersion,&response)!=noErr)||(response<0x700)){
  389.             err=unimpErr;
  390.         }
  391.         
  392.         if (err==noErr){
  393.             globals=(icteGlobalsPtr)NewPtrSysClear(sizeof(icteGlobals));
  394.             if (globals==(icteGlobalsPtr)0)
  395.                 err=MemError();
  396.         }
  397.         
  398.         if (err==noErr){
  399.             SetMyGlobals(globals);
  400.             globals=GetMyGlobals();
  401.             
  402.             // init globals
  403.             globals->signature=kCreator;
  404.             
  405.             vers=(VersRecHndl)Get1Resource('vers',1);
  406.             if (vers!=(VersRecHndl)0){
  407.                 HLock((Handle)vers);
  408.                 globals->version=(*vers)->numericVersion;
  409.                 HUnlock((Handle)vers);
  410.             }
  411.             
  412.             exclusions=Get1Resource('EXCL',rExclusions);
  413.             err=HandToHand(&exclusions);
  414.             
  415.             globals->exclusions=(exHandle)exclusions;
  416.             
  417.             errors=Get1Resource('STR#',rErrorStrings);
  418.             err2=HandToHand(&errors);
  419.             
  420.             globals->errors=errors;
  421.             
  422.             if (err==noErr)
  423.                 err=err2;
  424.         }
  425.         
  426.         if (err==noErr){
  427.             SelectorFunctionUPP sel;
  428.             
  429.             sel=NewSelectorFunctionProc(MyGestalt);
  430.             
  431.             err=NewGestalt(kCreator,sel);
  432.         }
  433.         
  434.         if (err==noErr){
  435.             // install the patch
  436.             UniversalProcPtr nteclick;
  437.             
  438.             globals->old_teclick=(TEClickUPP)NGetTrapAddress(_TEClick,ToolTrap);
  439.             
  440.             nteclick=(UniversalProcPtr)NewTEClickProc(PascalTEClickPatch);
  441.             
  442.             NSetTrapAddress(nteclick,_TEClick,ToolTrap);
  443.         }
  444.         
  445.         /*
  446.             If we got an error then we bleed memory all over the place, this is not
  447.             an accident.  How many copies of the init can you reasonably fail to install?
  448.         */
  449.         
  450.         SetZone(ozone);
  451.         
  452.         if (err==noErr){
  453.             ShowInitIcon(rICTEIcon,true);
  454.         } else {
  455.             ShowInitIcon(rFailedIcon,true);
  456.         }
  457.     }
  458. #if defined(powerc) || defined (__powerc)
  459.     __rsrcterm();
  460.     
  461. #else
  462.     RestoreA4();
  463. #endif
  464. }
  465.  
  466.